﻿using System;

class BinaryRepresentationOfShort
{
    static void Main()
    {
        int shortNum = -256;
        string binary = ConvertDeciToBin(shortNum);

        Console.WriteLine("Decimal: " + shortNum);
        Console.WriteLine("Binary:  " + binary);
    }

    private static string ConvertDeciToBin(int shortNum)
    {
        string binStr = String.Empty;
        int[] binaryArr = new int[16];
        int remainder = 0;
        int numBase = 2;
        bool negative = false;

        if (shortNum < 0)
        {
            negative = true;
            shortNum = (shortNum * (-1)) - 1; // -n = ~(n-1)
        }

        // Filling the array with "1" and "0"
        for (int i = 0; (shortNum != 0) && (i < 16); i++)
        {
            remainder = shortNum % numBase;
            binaryArr[i] = remainder;
            shortNum = shortNum / numBase;
        }

        // If the number is negative its bits are inverted
        if (negative)
        {
            InvertBits(binaryArr);
        }

        return ConvertArrayToString(binaryArr);
    }    

    private static void InvertBits(int[] binaryArr)
    {
        for (int i = 15; i >= 0; i--)
        {
            if (binaryArr[i] == 0)
            {
                binaryArr[i] = 1;
            }
            else
            {
                binaryArr[i] = 0;
            }
        }
    }

    private static string ConvertArrayToString(int[] binaryArr)
    {
        string binStr = String.Empty;

        for (int i = 15; i >= 0; i--)
        {
            binStr += binaryArr[i].ToString();
        }

        return binStr;
    }
}